package com.fanghaoran.view.user.studyPlanFrame;

import com.fanghaoran.controller.InfoController;
import com.fanghaoran.controller.SignController;
import com.fanghaoran.controller.StudyInfoController;
import com.fanghaoran.entity.Sign;
import com.fanghaoran.entity.Users;
import com.fanghaoran.view.LoginFrame;

import javax.swing.*;
import java.io.IOException;

/**
 * @Author: win11
 * @Date: 2023/03/18/9:44
 * @Description: 学习情况页面
 */
public class StudyPlanPanel extends JPanel {
    /**
     * 常量1->表示已签到
     */
    public  static final int SIGNED=1;

    /**
     * 用户的个人信息
     */
    public static Users user;
    static {
        try {
            user = (Users)new InfoController().getInfo(LoginFrame.usernameInput).getMessage();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 用户的签到数据
     */
    public Sign sign = (Sign) new SignController().selectSignInfo().getMessage();

    /**
     * 用户的总学习时间数据
     */
    private final int totalStudyTime=(int)new StudyInfoController().getStudyTime(user.getId()).getMessage();

    /**
    * 获取已学单词数
    */
    public static int wordAmount;
    static {
        try {
            wordAmount = (int) new StudyInfoController().getWordAmount(user.getId()).getMessage();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取计划
     */
    private final int studyPlan=(int)new StudyInfoController().getStudyPlan(user.getId()).getMessage();

    public static JLabel plan;

    public StudyPlanPanel() throws Exception {


        JPanel panel=this;
        //垂直布局
        Box vBox=Box.createVerticalBox();
        vBox.add(Box.createVerticalStrut(100));

        //水平布局
        Box box1=Box.createHorizontalBox();
        //学习总单词数
        JLabel wordAccountText=new JLabel("学习总单词数: ");
        JLabel wordAccount=new JLabel(wordAmount+"个");
        box1.add(wordAccountText);
        box1.add(Box.createHorizontalStrut(20));
        box1.add(wordAccount);

        Box box2=Box.createHorizontalBox();
        //学习总时间

        JLabel totalTimeText=new JLabel("学习总时间: ");
        JLabel totalTime=new JLabel(totalStudyTime+"分钟");
        box2.add(totalTimeText);
        box2.add(Box.createHorizontalStrut(20));
        box2.add(totalTime);

        Box box3=Box.createHorizontalBox();
        //连续打卡天数
        JLabel signAccountText=new JLabel("连续打卡天数: ");
        JLabel signAccount=new JLabel();
        if(sign==null){
            signAccount.setText("0 天");
        }else{
            signAccount=new JLabel(sign.getClockDays()+" 天");
        }
        box3.add(signAccountText);
        box3.add(Box.createHorizontalStrut(20));
        box3.add(signAccount);

        //今日计划
        Box box5=Box.createHorizontalBox();
        JLabel planText=new JLabel("今日计划学习单词数:   ");
        plan=new JLabel(studyPlan+"个");
        box5.add(planText);
        box5.add(plan);
        Box box4=Box.createHorizontalBox();

        Box box6=Box.createHorizontalBox();
        JButton modifyPlanBtn=new JButton("调整今日学习计划");
        box6.add(modifyPlanBtn);

        //打卡按钮
        String signBtnText;
        //signFlag 1:表示已打卡
        //           0:表示未打卡
        if(sign!=null&&sign.getFlag()==SIGNED){
            signBtnText="今日已打卡";
        }else{
            signBtnText="今日打卡";
        }
        //设置按钮文字
        JButton signBtn=new JButton(signBtnText);
        box4.add(signBtn);



        //点击事件
        signBtn.addActionListener(e-> {
                try {
                    if("今日已打卡".equals(signBtn.getText())){
                        JOptionPane.showMessageDialog(panel,"您今日已打卡");
                    }else{
                        //存储打卡信息的业务操作
                        new SignController().updateSignInfo(sign);
                        JOptionPane.showMessageDialog(panel,"打卡成功!");
                        signBtn.setText("今日已打卡");
                        //指定时间更新打卡flag
                        new SignController().updateFlag(sign);

                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }

            }
        );
        //调整学习计划
        modifyPlanBtn.addActionListener(e-> {
                    try {
                        new ModifyPlanFrame();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
        );

        vBox.add(box1);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(box2);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(box3);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(box5);
        vBox.add(Box.createVerticalStrut(50));
        vBox.add(box6);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(box4);


        this.add(vBox);
    }
}
